/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.BaoCao;

import BUS.CTPhieuThuePhong_BUS;
import BUS.HoaDon_BUS;
import BUS.KhachHang_BUS;
import BUS.PhieuDatTiec_BUS;
import BUS.PhieuDichVu_BUS;
import BUS.PhieuHuHai_BUS;
import BUS.PhieuThuePhong_BUS;
import BUS.Phong_BUS;
import DTO.CTPhieuThuePhong_DTO;
import DTO.HoaDon_DTO;
import DTO.KhachHang_DTO;
import GUI.ShowReport;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public class JIFLapHoaDon extends javax.swing.JInternalFrame {

    DefaultTableModel modelChiPhi;

    /**
     * Creates new form JIFLapHoaDon
     */
    public JIFLapHoaDon() {
        initComponents();
        this.setSize(880, 560);
        showCbbMaKhach_hd();
        showTableChiPhi();
        showHoaDon();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnHoaDon = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        cbbMaKhach_hd = new javax.swing.JComboBox();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        tfTenKhach_hd = new javax.swing.JTextField();
        jLabel30 = new javax.swing.JLabel();
        tfSoDienThoai_hd = new javax.swing.JTextField();
        jLabel31 = new javax.swing.JLabel();
        tfTriGia_hd = new javax.swing.JTextField();
        btLapPhieu_hd = new javax.swing.JButton();
        btThoat_hd = new javax.swing.JButton();
        jLabel32 = new javax.swing.JLabel();
        dpNgayThanhToan_hd = new org.jdesktop.swingx.JXDatePicker();
        btLapMoi_hd = new javax.swing.JButton();
        jScrollPane8 = new javax.swing.JScrollPane();
        tbCacKhoanPhi = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("LẬP HÓA ĐƠN THANH TOÁN");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin Khách hàng"));
        jPanel6.setToolTipText("");
        jPanel6.setName("Thông tin Khách hàng"); // NOI18N
        jPanel6.setPreferredSize(new java.awt.Dimension(237, 110));

        cbbMaKhach_hd.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbMaKhach_hdItemStateChanged(evt);
            }
        });
        cbbMaKhach_hd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbMaKhach_hdActionPerformed(evt);
            }
        });

        jLabel28.setText("Mã Khách");

        jLabel29.setText("Tên Khách Hàng");

        tfTenKhach_hd.setEditable(false);

        jLabel30.setText("Số Điện Thoại");

        tfSoDienThoai_hd.setEditable(false);

        jLabel31.setText("Trị Giá:");

        tfTriGia_hd.setEditable(false);

        btLapPhieu_hd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Dollar.png"))); // NOI18N
        btLapPhieu_hd.setText("Lập hóa đơn");
        btLapPhieu_hd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieu_hdActionPerformed(evt);
            }
        });

        btThoat_hd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat_hd.setText("Thoát");
        btThoat_hd.setMaximumSize(new java.awt.Dimension(91, 23));
        btThoat_hd.setMinimumSize(new java.awt.Dimension(91, 23));
        btThoat_hd.setPreferredSize(new java.awt.Dimension(91, 23));
        btThoat_hd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoat_hdActionPerformed(evt);
            }
        });

        jLabel32.setText("Ngày Thanh Toán");

        dpNgayThanhToan_hd.setDate(new Date (System.currentTimeMillis()));

        btLapMoi_hd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btLapMoi_hd.setText("Làm mới");
        btLapMoi_hd.setMaximumSize(new java.awt.Dimension(91, 23));
        btLapMoi_hd.setMinimumSize(new java.awt.Dimension(91, 23));
        btLapMoi_hd.setPreferredSize(new java.awt.Dimension(91, 23));
        btLapMoi_hd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapMoi_hdActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jLabel32)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dpNgayThanhToan_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(67, 67, 67)
                                .addComponent(jLabel31))
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addComponent(jLabel29)
                                        .addGap(18, 18, 18)
                                        .addComponent(tfTenKhach_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addComponent(jLabel28)
                                        .addGap(18, 18, 18)
                                        .addComponent(cbbMaKhach_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel30)))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfSoDienThoai_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfTriGia_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(btLapMoi_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btLapPhieu_hd)
                        .addGap(18, 18, 18)
                        .addComponent(btThoat_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfSoDienThoai_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel30))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel28)
                            .addComponent(cbbMaKhach_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTenKhach_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel29))))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel32)
                    .addComponent(dpNgayThanhToan_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTriGia_hd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel31))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btLapPhieu_hd)
                    .addComponent(btThoat_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btLapMoi_hd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        jScrollPane8.setBorder(javax.swing.BorderFactory.createTitledBorder("Các khoản phí"));

        tbCacKhoanPhi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbCacKhoanPhi.setEnabled(false);
        jScrollPane8.setViewportView(tbCacKhoanPhi);

        javax.swing.GroupLayout pnHoaDonLayout = new javax.swing.GroupLayout(pnHoaDon);
        pnHoaDon.setLayout(pnHoaDonLayout);
        pnHoaDonLayout.setHorizontalGroup(
            pnHoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnHoaDonLayout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(pnHoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane8)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        pnHoaDonLayout.setVerticalGroup(
            pnHoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnHoaDonLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 851, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 531, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(29, Short.MAX_VALUE)
                    .addComponent(pnHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbMaKhach_hdItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbMaKhach_hdItemStateChanged
        if (cbbMaKhach_hd.getSelectedItem() == null) {
            return;
        }
        try {
            // TODO add your handling code here:
            tfTenKhach_hd.setText(KhachHang_BUS.layTenKHTheoMa(cbbMaKhach_hd.getSelectedItem().toString()));
            tfSoDienThoai_hd.setText(KhachHang_BUS.laySoDTTheoMa(cbbMaKhach_hd.getSelectedItem().toString()));
        } catch (Exception ex) {
            Logger.getLogger(JIFLapHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        showTableChiPhi();
        showHoaDon();
    }//GEN-LAST:event_cbbMaKhach_hdItemStateChanged

    private void cbbMaKhach_hdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbMaKhach_hdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbbMaKhach_hdActionPerformed

    private void btLapPhieu_hdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieu_hdActionPerformed
        // TODO add your handling code here:
        try {
            String mahdMax;
            String mahd;
            mahdMax = HoaDon_BUS.laymaMax();

            if (mahdMax.equals("")) {
                mahd = Phong_BUS.taoMaTuDong("MHD000001", 3);
            } else {
                mahd = Phong_BUS.taoMaTuDong(mahdMax, 3);
            }

            SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
            Date dngayden = dpNgayThanhToan_hd.getDate();
            String ngaythanhtoan = format.format(dngayden);
            HoaDon_DTO h = new HoaDon_DTO();
            h.setMahoaDon(mahd);
            h.setMaKhach(cbbMaKhach_hd.getSelectedItem().toString());
            h.setNgaythanhToan(ngaythanhtoan);
            h.setTriGia(tfTriGia_hd.getText());
            h.setPhiThuePhong(tbCacKhoanPhi.getValueAt(0, 0).toString());
            h.setPhiDichVu(tbCacKhoanPhi.getValueAt(0, 1).toString());
            h.setPhiDatTiec(tbCacKhoanPhi.getValueAt(0, 2).toString());
            h.setPhiHuHai(tbCacKhoanPhi.getValueAt(0, 3).toString());
            HoaDon_BUS.Insert(h);
            ArrayList<CTPhieuThuePhong_DTO> ds = new ArrayList<>();
            ds = CTPhieuThuePhong_BUS.layDsPhongThue(cbbMaKhach_hd.getSelectedItem().toString());

            for (int i = 0; i < ds.size(); i++) {
                String maPhong = ds.get(i).getMaPhong().toString();
                Phong_BUS.capnhattinhtrangDat(maPhong, "Trống");

            }
            KhachHang_BUS.capNhatTinhTrangKhach(cbbMaKhach_hd.getSelectedItem().toString(), "Đã thanh toán");
            JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
            new ShowReport().Show_ReportHoaDon(cbbMaKhach_hd.getSelectedItem().toString());
            cbbMaKhach_hd.removeAllItems();
            btLapMoi_hdActionPerformed(evt);
            showCbbMaKhach_hd();
        } catch (Exception ex) {
            Logger.getLogger(JIFLapHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btLapPhieu_hdActionPerformed

    private void btThoat_hdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoat_hdActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoat_hdActionPerformed

    private void btLapMoi_hdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapMoi_hdActionPerformed
        // TODO add your handling code here:
        tfTenKhach_hd.setText("");
        tfSoDienThoai_hd.setText("");
        tfTriGia_hd.setText("");
        modelChiPhi.setNumRows(0);
    }//GEN-LAST:event_btLapMoi_hdActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLapMoi_hd;
    private javax.swing.JButton btLapPhieu_hd;
    private javax.swing.JButton btThoat_hd;
    private javax.swing.JComboBox cbbMaKhach_hd;
    private org.jdesktop.swingx.JXDatePicker dpNgayThanhToan_hd;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JPanel pnHoaDon;
    private javax.swing.JTable tbCacKhoanPhi;
    private javax.swing.JTextField tfSoDienThoai_hd;
    private javax.swing.JTextField tfTenKhach_hd;
    private javax.swing.JTextField tfTriGia_hd;
    // End of variables declaration//GEN-END:variables

    private void showHoaDon() {
        if (cbbMaKhach_hd.getSelectedItem() == null) {
            return;
        }
        float trigia_phong;
        try {
            trigia_phong = PhieuThuePhong_BUS.layTriGiaTheoMaKhach(cbbMaKhach_hd.getSelectedItem().toString());
            Float trigia_dichvu = PhieuDichVu_BUS.layTriGiaTheoMaKhach(cbbMaKhach_hd.getSelectedItem().toString());
            Float trigia_tiec = PhieuDatTiec_BUS.layTriGiaTheoMaKhach(cbbMaKhach_hd.getSelectedItem().toString());
            Float trigia_huhai = PhieuHuHai_BUS.layTriGiaTheoMaKhach(cbbMaKhach_hd.getSelectedItem().toString());
            Float triGia = trigia_phong + trigia_dichvu + trigia_tiec + trigia_huhai;
            Vector rowData = new Vector();
            rowData.add(String.format("%f",trigia_phong));
            rowData.add(String.format("%f",trigia_dichvu));
            rowData.add(String.format("%f",trigia_tiec));
            rowData.add(String.format("%f",trigia_huhai));
            if (rowData == null) {
                return;
            }
            modelChiPhi.addRow(rowData);
            tbCacKhoanPhi.setModel(modelChiPhi);
            String strTG = String.format("%f", triGia);
            tfTriGia_hd.setText(strTG);
        } catch (Exception ex) {
            Logger.getLogger(JIFLapHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void showCbbMaKhach_hd() {
        DefaultComboBoxModel cbbModel;
        ArrayList<KhachHang_DTO> ds = new ArrayList<>();
        cbbModel = (DefaultComboBoxModel) cbbMaKhach_hd.getModel();
        try {
            ds = KhachHang_BUS.layKhachDangThue();
        } catch (Exception e) {
        }

        for (KhachHang_DTO lp : ds) {
            cbbModel.addElement(lp.getMaKhachHang());

        }
    }
    public void showTableChiPhi() {
        Vector column = new Vector();
        column.add("Tiền phòng");
        column.add("Tiền dịch vụ");
        column.add("Tiền đặt tiệc");
        column.add("Tiền hư hại");
        modelChiPhi = new DefaultTableModel(column, 0);
        tbCacKhoanPhi.setModel(modelChiPhi);
    }
}
